package com.huang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.dao.BlogMapper;
import com.huang.entity.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@ResponseBody
public class BlogController {
    @Autowired
    private BlogMapper blogMapper;
    @GetMapping ("/findAll")
    public List<Blog> selectAll(){
        List<Blog> blogs = blogMapper.queryAllBlog();
        for (Blog blog : blogs) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//定义新的日期格式
            blog.setDatetime(formatter.format(blog.getTime()));
        }
        return blogs;
    }
    @GetMapping("/findAll/{current}/{size}")
    public IPage<Blog> selectAll(@PathVariable("current") Integer current,
                                 @PathVariable("size") Integer size){
        Page<Blog> page = new Page<>(current,size);
        IPage<Blog> mapIPage = blogMapper.selectMyBlog(page);
        List<Blog> records = mapIPage.getRecords();
        for (Blog record : records) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            record.setDatetime(formatter.format(record.getTime()));
        }
        return mapIPage;
    }
    @GetMapping("findAll/{current}/{size}/{userid}")
    public IPage<Blog> selectAll(@PathVariable("current") Integer current,
                                 @PathVariable("size") Integer size,
                                 @PathVariable("userid") Integer userid){
        QueryWrapper<Blog> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userid);
        Page<Blog> page = new Page<>(current,size);
        IPage<Blog> blogIPage = blogMapper.queryBlogPage(page, wrapper);
        List<Blog> records = blogIPage.getRecords();
        for (Blog record : records) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            record.setDatetime(formatter.format(record.getTime()));
        }
        return blogIPage;
    }
    @GetMapping("/findBlog/{id}")
    public Blog selectBlog(@PathVariable("id") Integer id){
        Blog blog = blogMapper.queryBlogById(id);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//定义新的日期格式
        blog.setDatetime(formatter.format(blog.getTime()));
        return blog;
    }
    @PostMapping("/save")
    public String saveBlog(@RequestBody Blog blog){
        Date date = new Date();
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        blog.setTime(date1);
        int insert = blogMapper.saveBlog(blog);
        if (insert>0){
            return "success";
        }else {
            return "error";
        }
    }
    @PostMapping("/blog/update")
    public String updateBlog(@RequestBody Blog blog){
        Integer i = blogMapper.updateBlog(blog);
        if (i>0){
            return "success";
        }else {
            return "error";
        }
    }
    @DeleteMapping("/blog/delete/{id}")
    public String deleteBlog(@PathVariable("id") Integer id){
        int i = blogMapper.deleteById(id);
        if (i>0){
            return "success";
        }else {
            return "error";
        }
    }
    @GetMapping("blog/setpetadopt/{blogId}")
    public String setPetadopt(@PathVariable("blogId") Integer blogId){
        Blog blog = blogMapper.queryBlogById(blogId);
        blog.setPetadopt(1);
        Integer integer = blogMapper.updateBlog(blog);
        if (integer>0){
            return "success";
        }else {
            return "error";
        }
    }
}
